package cn.com.fuhuitong.main.home.ui.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeFragment
import cn.com.fuhuitong.main.home.adapter.CharityListAdapter
import cn.com.fuhuitong.main.home.vm.CharityViewModel
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.ItemViewType
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.fragment_home_charity.*

class HomeCharityFragment : ViewModeFragment<CharityViewModel>() {

    private var charityType: Int = 1

    companion object {
        private const val TYPE = "type"
        fun newInstance(type: Int): HomeCharityFragment {
            val fragment = HomeCharityFragment()
            fragment.arguments = Bundle().apply {
                putInt(TYPE, type)
            }
            return fragment
        }
    }

    private lateinit var charityListAdapter: CharityListAdapter

    override fun viewModel(): CharityViewModel = ViewModelProvider(
        this, ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
    ).get(CharityViewModel::class.java)

    override val layoutResId: Int
        get() = R.layout.fragment_home_charity

    override fun initView() {

        charityType = arguments?.getInt(TYPE) ?: 1

        this.initViewRecyclerAdapter(
            recycler_charity_list, arrayOf(ItemViewType.CHARITY_LIST)
        )

        delegateAdapter.addAdapter(
            CharityListAdapter(
                requireContext(), LinearLayoutHelper(resources.getDimensionPixelSize(R.dimen.dp_10))
            ).apply {
                charityListAdapter = this
            })

        charityListAdapter.setOnItemClickListener { view, i, charityListEntity ->
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_CHARITY_DETAILS)
                .withSerializable(AppConstance.BUNDLE_CHARITY_ITEM, charityListEntity)
                .navigation()
        }
    }

    override fun initViewMode() {
        viewModel.charityListDataLiveData.observe(viewLifecycleOwner, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                if (it.size == 0)
                    return@let
                charityListAdapter.setAddDataNew(it)
            }
        })
    }

    override fun initData() {
        viewModel.charityIndex(charityType)
    }

    override fun onLoadingEmptyView(state: EmptyState) {
        super.onLoadingEmptyView(state)
        viewModel.charityIndex(charityType)
    }


}